package com.caits.lbs.framework.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZipUtil {
	private static final int BUFFER = 1024;

	// 压缩
	/** * 数据压缩 * 
	 * * @param str 
	 * 			被压缩的内容
	 * * @param type 
	 * 			补充压缩类型,1 -base64压缩
	 * * @throws Exception */
	public static String compress(String str,int type )
			throws Exception {
		ByteArrayInputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		GZIPOutputStream gos = new GZIPOutputStream(os);
		int count;
		byte data[] = new byte[BUFFER];
		while ((count = is.read(data, 0, BUFFER)) != -1) {
			gos.write(data, 0, count);
			Arrays.fill(data,(byte)0);
		}
		gos.finish();
		gos.flush();
		gos.close();
		if(type==1){
//			return Base64.encode(os.toByteArray());
			return new String(Base64Codec.encode(os.toByteArray()));
		}
		
		return new String(os.toByteArray());
	}

	/** * 数据解压缩 * 
	 * * @param in_data 
	 * 			要解压的数据
	 * * @param type 
	 * 			补充解压类型,1-base64解压
	 * * @throws Exception */
	public static String decompress(String in_data,int type )
			throws Exception {
		byte[] b_data= in_data.getBytes("UTF-8");
		if(type == 1){
//			b_data	=Base64.decode(in_data);
			b_data = Base64Codec.decode(in_data.toCharArray());
		}
		ByteArrayInputStream is = new ByteArrayInputStream(b_data);
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		GZIPInputStream gis = new GZIPInputStream(is);
		int count;
		byte data[] = new byte[BUFFER];
		while ((count = gis.read(data, 0, BUFFER)) != -1) {
			os.write(data, 0, count);
			Arrays.fill(data,(byte)0);
		}
		gis.close();
		return new String(os.toByteArray(),"UTF-8");
	}
	/**
	 * 压缩二进制 
	 * @param str_arr
	 * @return
	 * @throws Exception
	 */
	public static byte[] gzipCompress(byte[] str_arr)
			throws Exception {
		ByteArrayInputStream is = new ByteArrayInputStream(str_arr);
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		GZIPOutputStream gos = new GZIPOutputStream(os);
		int count = 0;
		//缓冲区大小[1M<=BUFFER<=10M]
		int BUFFER = Math.min(Math.max(1024*1024,str_arr.length/10),10*1024*1024);
		byte data[] = new byte[BUFFER ];
		while ((count = is.read(data, 0, BUFFER)) != -1) {
			gos.write(data, 0, count);
			Arrays.fill(data,(byte)0);
		}
		gos.finish();
		gos.flush();
		gos.close();
		
		return os.toByteArray();
	}
	/**
	 * 解压gzip数据流 
	 * @param str_arr
	 * @return
	 * @throws Exception
	 */
	public static byte[] gzipDeCompress(byte[] str_arr)
			throws Exception {
		ByteArrayInputStream is = new ByteArrayInputStream(str_arr);
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		GZIPInputStream gis = new GZIPInputStream(is);
		int count = 0;
		int BUFFER = Math.min(Math.max(1024*1024,str_arr.length/10),10*1024*1024);
		byte data[] = new byte[BUFFER ];
		while ((count = gis.read(data, 0, BUFFER)) != -1) {
			os.write(data, 0, count);
			Arrays.fill(data,(byte)0);
		}
		os.flush();
		os.close();
		
		return os.toByteArray();
	}
	// 测试方法
	public static void main(String[] args) throws IOException {
		byte[] data = "我是中国china".getBytes();

		try {
			long l_start = System.currentTimeMillis();
			for(int i=0;i<10;i++){
			 data = gzipCompress(data);
			 String ret = ByteHelper.bytes2HexString(data,",");
			 System.out.println("压缩后的值："+ret);
			 
			 data = gzipDeCompress(data);
			 System.out.println("解压缩后的值："+new String(data));
			}
			System.out.println("time elipse :"+(System.currentTimeMillis()-l_start)+"ms");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
